﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
using ModuleDALCore;

namespace DAL
{
	public class TOATHUOCBENHNHANDAO : DALBase
	{
		public TOATHUOCBENHNHANDAO()
		{
			this.OpenConnect();
		}

		private DataTable dt;
		private string _SpName;

		public string SpName
		{
			get { return "SPTOATHUOCBENHNHAN"; }
			set { _SpName = value; }
		}

		public List<TOATHUOCBENHNHAN> GetListTH()
		{
			List<TOATHUOCBENHNHAN> TH = new List<TOATHUOCBENHNHAN>();
			dt = new DataTable();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(new TOATHUOCBENHNHAN(), null), ConvertClassToArray.GetValues(new TOATHUOCBENHNHAN(), 0));
				TH = (List<TOATHUOCBENHNHAN>)ConvertDatatableToList.ConvertTo<TOATHUOCBENHNHAN>(dt);
			}

			catch { return null; }

			return TH;
		}
		public DataTable GetListTHs()
		{
			dt = new DataTable();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(new TOATHUOCBENHNHAN(), null), ConvertClassToArray.GetValues(new TOATHUOCBENHNHAN(), 0));
			}

			catch { return null; }

			return dt;
		}

		public List<TOATHUOCBENHNHAN> GetListTHbyID(TOATHUOCBENHNHAN th)
		{
			dt = new DataTable();
			List<TOATHUOCBENHNHAN> TH = new List<TOATHUOCBENHNHAN>();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(th, null), ConvertClassToArray.GetValues(th, 1));
				TH = (List<TOATHUOCBENHNHAN>)ConvertDatatableToList.ConvertTo<TOATHUOCBENHNHAN>(dt);
			}

			catch { return null; }

			return TH;
		}

		public List<CT_TOATHUOCBN> GetListCTTTbyIDTOAS(TOATHUOCBENHNHAN th)
		{
			dt = new DataTable();
			List<CT_TOATHUOCBN> TH = new List<CT_TOATHUOCBN>();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(th, null), ConvertClassToArray.GetValues(th, 6));
				TH = (List<CT_TOATHUOCBN>)ConvertDatatableToList.ConvertTo<CT_TOATHUOCBN>(dt);
			}
			catch { return null; }
			return TH;
		}

		public DataTable GetListCTTTbyIDTOA(TOATHUOCBENHNHAN th)
		{
			dt = new DataTable();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(th, null), ConvertClassToArray.GetValues(th, 6));
			}
			catch { return null; }
			return dt;
		}
		
		public List<TOATHUOCBENHNHAN> GetListTHbyIDBN(TOATHUOCBENHNHAN th)
		{
			dt = new DataTable();
			List<TOATHUOCBENHNHAN> TH = new List<TOATHUOCBENHNHAN>();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(th, null), ConvertClassToArray.GetValues(th, 5));
				TH = (List<TOATHUOCBENHNHAN>)ConvertDatatableToList.ConvertTo<TOATHUOCBENHNHAN>(dt);
			}

			catch { return null; }

			return TH;
		}

		public bool UPDATE(TOATHUOCBENHNHAN TH)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(TH, null), ConvertClassToArray.GetValues(TH, 3)) == 1);

			}
			catch { return false; }
		}

		public bool DELETE(TOATHUOCBENHNHAN TH)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQuery(SpName, ConvertClassToArray.GetParamName(TH, null), ConvertClassToArray.GetValues(TH, 4)) == 1);

			}
			catch { return false; }
		}

		public DataTable GetDonthuocRPT(TOATHUOCBENHNHAN th)
		{
			dt = new DataTable();

			try
			{
				dt = DBManager.Intance.ExecuteDataTable(SpName, ConvertClassToArray.GetParamName(th, null), ConvertClassToArray.GetValues(th, 7));
			}
			catch { return null; }
			return dt;
		}

		public void BEGINTRANSACTION()
		{
			DBManager.Intance.BeginTransaction();
		}
		public void CLOSE()
		{
			DBManager.Intance.CloseConnection();
		}
		public void COMMIT()
		{
			DBManager.Intance.CommitTransaction();
		}
		public void ROLLBACK()
		{
			DBManager.Intance.RollbackTransaction();
		}

		#region INSERT DATA TOATHUOC AND CHITHIETTOATHUOC

		public long INSERTTOA(TOATHUOCBENHNHAN tt)
		{
			long idtoa = 0;
			
			try
			{
				idtoa = long.Parse(DBManager.Intance.ExecuteNonQueryWithTransaction(SpName, ConvertClassToArray.GetParamName(tt, null), ConvertClassToArray.GetValues(tt, 2)).ToString());
				return idtoa;
			}
			catch { return 0; }
		}

		public bool INSERTCHITIETTOA(CT_TOATHUOCBN ct)
		{
			try
			{
				return (DBManager.Intance.ExecuteNonQueryWithTransaction("SPCT_TOATHUOCBN", ConvertClassToArray.GetParamName(ct, null), ConvertClassToArray.GetValues(ct, 1)) == 1);
			}
			catch { return false; }
		}

		public bool INSERT(TOATHUOCBENHNHAN toa, List<CT_TOATHUOCBN> lct)
		{
			bool res = true;
			long idtoa = 0;
			try
			{
				BEGINTRANSACTION();
				//insert toa thuoc truoc tien
				idtoa = INSERTTOA(toa);
				if (idtoa > 0)
				{
					foreach(CT_TOATHUOCBN ct in lct)
					{
						ct.IDTOATHUOC = idtoa;
						if (!INSERTCHITIETTOA(ct))
						{
							res = false;
							break;
						}
					}
				}
				else //idtoa = 0
				{
					res = false;
				}
				if (res)
				{
					COMMIT();
				}
				else
				{
					ROLLBACK();
				}
			}
			catch
			{
				ROLLBACK();
				res = false;
			}
			this.CLOSE();
			return res;
		}
		#endregion
	}
}